#include "playlistselectiondialog.h"
#include <QDebug>


PlaylistSelectionDialog::PlaylistSelectionDialog(QWidget * parent) : KeyControllableMenu(parent) {

    QVBoxLayout * mainLayout = new QVBoxLayout();
    QHBoxLayout * buttonsLayout = new QHBoxLayout();
    QWidget * mainWidget=new QWidget(this);
    QLabel * selectPlaylistLabel = new QLabel(this);

    playlistsListWidget = new QListWidget(this);
    createPlaylistLineEdit = new QLineEdit(this);
    okButton = new Button(QIcon(":/imgs/3F00Button_Ok.png"),"",this);
    cancelButton = new Button(QIcon(":/imgs/3F00Button_Cancel.png"),"",this);
    chosenPlaylist = new QString("");

    selectPlaylistLabel->setText("Select a Playlist:");
    createPlaylistLineEdit->setText("Create Playlist");
    okButton->setIconSize(QSize(103,60));
    cancelButton->setIconSize(QSize(103,60));

    buttonsLayout->addWidget(cancelButton);
    buttonsLayout->addWidget(okButton);
    mainLayout->addWidget(selectPlaylistLabel);
    mainLayout->addWidget(playlistsListWidget);
    mainLayout->addWidget(createPlaylistLineEdit);
    mainLayout->addLayout(buttonsLayout);


    mainWidget->setLayout(mainLayout);
    setLayout(new QVBoxLayout());
    layout()->addWidget(mainWidget);


    buttonsLayout->setDirection(QBoxLayout::RightToLeft);
    setMinimumSize(400,300);
    setStyleSheet("PlaylistSelectionDialog { background: rgb(0, 0, 0); color: rgb(255,255,255); } .Button{background: rgb(0, 0, 0);border:0px;} .Button:pressed {background: qlineargradient(spread:pad, x1:0.07, y1:0.153409, x2:1, y2:1, stop:0 rgba(0, 0, 0, 50), stop:1 rgba(255, 255, 255, 70)); border: 1px solid rgba(255, 255, 255, 200);} .QWidget {background: rgb(0, 0, 0);border: 1px solid white; border-radius: 10px;} .QLineEdit {color: rgb(220,220,220)} .QLineEdit:focus {color: rgb(0,0,0)} .QListWidget {background: rgb(0,0,0); border: 1px solid rgb(255,255,255)}");


    this->move(QPoint((QApplication::desktop()->width()-this->width())/2,(QApplication::desktop()->height()-this->height())/2));

    connect(createPlaylistLineEdit,SIGNAL(returnPressed()),this,SLOT(createPlaylist()));
    connect(createPlaylistLineEdit,SIGNAL(lostFocus()),this,SLOT(resetCreatePlaylistLineEdit()));

    connect(okButton,SIGNAL(clicked()),this,SLOT(okSelected()));
    connect(cancelButton,SIGNAL(clicked()),this,SLOT(cancelSelected()));

}


void PlaylistSelectionDialog::show() {
    playlistsListWidget->clear();
    QSqlQuery query = Querier::getResult("SELECT DISTINCT playlistName FROM Playlists ORDER BY playlistName");
    while(query.next()) {
        playlistsListWidget->addItem(query.value(0).toString());
    }
    connect(QApplication::instance(),SIGNAL(focusChanged(QWidget *, QWidget *)),this,SLOT(focusCreatePlaylistLineEdit(QWidget *, QWidget *)));
    QWidget::show();
    playlistsListWidget->setFocus();
}

void PlaylistSelectionDialog::closeEvent(QCloseEvent *event) {
    QWidget::closeEvent(event);
    disconnect(QApplication::instance(),SIGNAL(focusChanged(QWidget *, QWidget *)),this,SLOT(focusCreatePlaylistLineEdit(QWidget *, QWidget *)));
    emit closed(0);
}

void PlaylistSelectionDialog::okSelected() {
    hide();
    disconnect(QApplication::instance(),SIGNAL(focusChanged(QWidget *, QWidget *)),this,SLOT(focusCreatePlaylistLineEdit(QWidget *, QWidget *)));
    delete chosenPlaylist;
    chosenPlaylist = (playlistsListWidget->currentItem()) ? (new QString(playlistsListWidget->currentItem()->text())) : 0;
    emit closed(chosenPlaylist);
}

void PlaylistSelectionDialog::cancelSelected() {
    hide();
    disconnect(QApplication::instance(),SIGNAL(focusChanged(QWidget *, QWidget *)),this,SLOT(focusCreatePlaylistLineEdit(QWidget *, QWidget *)));
    emit closed(0);
}

void PlaylistSelectionDialog::createPlaylist(){
    if(createPlaylistLineEdit->text().trimmed().size()){
        QSqlQuery query = Querier::getResult("INSERT INTO Playlists (playlistName,mediaID) VALUES('"+createPlaylistLineEdit->text().trimmed()+"',0)");
        query = Querier::getResult("SELECT DISTINCT playlistName FROM Playlists ORDER BY playlistName");
        playlistsListWidget->clear();
        while(query.next()) {
            playlistsListWidget->addItem(query.value(0).toString());
        }
        createPlaylistLineEdit->setText("");
    }
}

void PlaylistSelectionDialog::focusCreatePlaylistLineEdit(QWidget * old, QWidget * now){
    if(old==createPlaylistLineEdit) createPlaylistLineEdit->setText("Create Playlist");
    else if(now==createPlaylistLineEdit) createPlaylistLineEdit->setText("");
}

